package com.bkgy.modular.tbdsjplancheckpdiC621.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.calsidata.entity.TbEdge;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.modular.datacollect_bx.bean.JDataSave621;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjplancheckpdiC621.dao.PlanCheckPdi621Dao;
import com.bkgy.modular.tbdsjplancheckpdiC621.entity.ExportPlanCheckPdi621;
import com.bkgy.modular.tbdsjplancheckpdiC621.entity.PlanCheckPdi621;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlanCheckPdi621Service {

    @Resource
    private PlanCheckPdi621Dao planCheckPdi621Dao;
    @Resource
    private MongoTemplate mongoTemplate;

    public ResultData findDataByPage(JSONObject json) {
        try {
            // Integer pageIndex = json.getInteger("pageIndex");
            //Integer pageSize = json.getInteger("pageSize");
            //if (pageIndex == null || pageSize == null) {
                //return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            //}
            //查询条件
            String hot_coil_id = null;
            String plan_no = null;
            String dbegin = null;
            String dend = null;
            Double trim_warn = null;
            JSONObject condition = json.getJSONObject("condition");
            if (!condition.isEmpty()) {
                if (StringUtils.hasText(condition.getString("hot_coil_id"))) {
                    hot_coil_id = condition.getString("hot_coil_id");
                }
                if (StringUtils.hasText(condition.getString("plan_no"))) {
                    plan_no = condition.getString("plan_no");
                }
                if (StringUtils.hasText(condition.getString("dbegin"))) {
                    dbegin = condition.getString("dbegin");
                }
                if (StringUtils.hasText(condition.getString("dend"))) {
                    dend = condition.getString("dend");
                }
                if (StringUtils.hasText(condition.getString("trim_warn"))) {
                    trim_warn = condition.getDouble("trim_warn");
                }
            }
            // (pageIndex - 1) * pageSize, pageIndex * pageSize,
            List<PlanCheckPdi621> list = planCheckPdi621Dao.findDataByPage(dbegin, dend, hot_coil_id, plan_no,trim_warn);
            Integer count = planCheckPdi621Dao.findDataCount(dbegin, dend, hot_coil_id, plan_no,trim_warn);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        // Integer pageSize = jsonObject.getInteger("pageSize");
        // Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        String hot_coil_id = null;
        String plan_no = null;
        String dbegin = null;
        String dend = null;
        Double trim_warn = null;
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (!condition.isEmpty()) {
            if (StringUtils.hasText(condition.getString("hot_coil_id"))) {
                hot_coil_id = condition.getString("hot_coil_id");
            }
            if (StringUtils.hasText(condition.getString("plan_no"))) {
                plan_no = condition.getString("plan_no");
            }
            if (StringUtils.hasText(condition.getString("dbegin"))) {
                dbegin = condition.getString("dbegin");
            }
            if (StringUtils.hasText(condition.getString("dend"))) {
                dend = condition.getString("dend");
            }
            if (StringUtils.hasText(condition.getString("trim_warn"))) {
                trim_warn = condition.getDouble("trim_warn");
            }
        }
        // 创建表格数据集合
        List<PlanCheckPdi621> listExcel = new ArrayList<>();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<PlanCheckPdi621> data = planCheckPdi621Dao.findDataByPage(dbegin, dend, hot_coil_id, plan_no,trim_warn);
        for (PlanCheckPdi621 item : data) {
            listExcel.add(item);
        }
        // 判断service查询出的数据集合data是否为空
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportPlanCheckPdi621> list = new ArrayList<>();
        for (PlanCheckPdi621 item : listExcel) {
            ExportPlanCheckPdi621 exportPlanCheckPdi621 = new ExportPlanCheckPdi621();
            BeanUtils.copyProperties(item,exportPlanCheckPdi621);
            list.add(exportPlanCheckPdi621);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\1720生产计划审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    public ResultData findLineDataByVolumeNo(JSONObject json) {
        String volumeNo = json.getString("volumeNo");
        if (!StringUtils.hasText(volumeNo)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }

        Map<String, Map<String, Object>> map = new HashMap<>();
        Query query1 = new Query(Criteria.where("volumeNo").is(volumeNo));
        QmsCurveDataMongo qmsCurveDataMongo = mongoTemplate.findOne(query1, QmsCurveDataMongo.class);

        Map<String, Object> mapValue = new HashMap<>();
        if(!ObjectUtils.isEmpty(qmsCurveDataMongo)){
            List<Double> list1 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdh_thk_cold");
            List<Double> list2 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdw_width_cold");
            List<Double> list3 = qmsCurveDataMongo.getOriginBolg().get("hsm_crown_c40");
            List<Double> list4 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_wedge40");
            List<Double> list5 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdt_fdt");
            List<Double> list6 = qmsCurveDataMongo.getOriginBolg().get("hsm_ctc_ct_temp");
            List<Double> list7 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_symmetric_flatness");
            List<Double> list8 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_asymmetric_flatness");

            mapValue.put("hsm_fdh_thk_cold", list1);
            mapValue.put("hsm_fdw_width_cold", list2);
            mapValue.put("hsm_crown_c40", list3);
            mapValue.put("hsm_flatness_wedge40", list4);
            mapValue.put("hsm_fdt_fdt", list5);
            mapValue.put("hsm_ctc_ct_temp", list6);
            mapValue.put("hsm_flatness_symmetric_flatness", list7);
            mapValue.put("hsm_flatness_asymmetric_flatness", list8);
        }

        map.put(volumeNo, mapValue);
        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }

    /**
     * 根据卷号查询模板数据
     * @return
     */
    public ResultData findTemplateData(JSONObject json) {
        // 创建query查询条件,根据extid(卷号)进行查询
        Query query = new Query(Criteria.where("extid").is(json.getString("extid")));
        TbEdge tbEdge = mongoTemplate.findOne(query, TbEdge.class);
        JDataSave621 jDataSave621 = mongoTemplate.findOne(query, JDataSave621.class);
        if(!ObjectUtils.isEmpty(jDataSave621.getRow().get("c621_tcm_shape_channel15"))){
            // 查询到的数据外层套mongdb,mongxian
            Map<String,Object> map = new HashMap<>();
            map.put("mongxian",jDataSave621.getRow());
            Map<String,Map<String,Object>> list = new HashMap<>();
            list.put("mongdb",map);
            return ResultData.ResultDataSuccess(list);
        } else if (tbEdge != null) {
            // 查询到的数据外层套mongdb,mongxian
            Map<String,Object> map = new HashMap<>();
            map.put("mongxian",tbEdge.getEdge());

            Map<String,Map<String,Object>> list = new HashMap<>();
            list.put("mongdb",map);
            return ResultData.ResultDataSuccess(list);
        } else {
            return ResultData.ResultDataFaultSelf("查询数据为空",null);
        }
    }

    /**
     * 2130根据卷号查询模板数据
     * @param json
     * @return
     */
    public ResultData findMongoDbData(JSONObject json) {
        // 创建query查询条件,根据extid(卷号)进行查询
        Query query = new Query(Criteria.where("extid").is(json.getString("extid")));
        TbFalt tbFalt = mongoTemplate.findOne(query, TbFalt.class);

        if (tbFalt != null) {
            // 查询到的数据外层套mongdb,mongxian
            Map<String,Object> map = new HashMap<>();
            map.put("mongxian",tbFalt.getEdge());

            Map<String,Map<String,Object>> list = new HashMap<>();
            list.put("mongdb",map);
            return ResultData.ResultDataSuccess(list);
        } else {
            return ResultData.ResultDataFaultSelf("查询数据为空",null);
        }
    }
}
